<?php

class UrlService extends AbstractService
{

    protected function _init()
    {
	$this->setMapper(new UrlMapper());
    }

    public function encode($url)
    {
	return md5($url);
    }

    public function getByHash($hash)
    {
	if (!$hash) {
	    return false;
	}

	return $this->getMapper()->getBy('hash', $hash);
    }

    public function getByReview(ReviewModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}

	$cacheSlot = new CacheSlotsReviewFeatures($model, 'urls');
	if (($urls = $cacheSlot->load()) === false) {
	    $urls = $this->getMapper()->getAllFeatures($model->getId(), REVIEW_TABLE, URL_TABLE);
	    $cacheSlot->setData($urls)->save();
	}

	if ($urls) {
	    $model->setUrls($urls);
	}
    }

}